#ifndef _OS_QUEUE_H_
#define _OS_QUEUE_H_

#include "queue.h"


#define MAX_OS_QUEUES 512

/*
* Information maintained by queue about its subscribers
*
typedef struct _q_subscriber_t {
    uint32_t pid;		// subscriber id

    void (*disconnect_handler)(uint32_t);  // disconnect handler of subscriber
    uint32_t disconnect_param;             // parameter for disconnect handler

    struct _q_subscriber_t *next_subscriber;  // Next subscriber to the queue
}_q_subscriber_t;
*/

/*
* Complete information maintained by a queue
*/
typedef struct _q_data_t {

    queue_handle_t q_handle;
    uint32_t   owner;    // PID
    q_id_t queue_id;

    struct _q_data_t *next_queue;    // Next queue owned by the process

    //_q_subscriber_t *subscribers;   // Peer processes enqueuing to this queue
}q_data_t;


/*
* Free-list of queue_ids
*/
typedef struct _q_freelist_t
{
  uint16_t index;
  uint16_t run_counter;

  q_data_t *q_data;
  struct _q_freelist_t *next_free;
}_q_id_list_t;

#define GET_QUEUE_ID(q_id_ptr) (((uint32_t)q_id_ptr->index << 16) | (q_id_ptr->run_counter & 0xFFFF))
#define GET_QUEUE_RUN_COUNTER(q_id) (q_id & 0xFFFF)
#define GET_QUEUE_INDEX(q_id) ((q_id >> 16) & 0xFFFF)


/*
* Information maintained by a subscriber about the queues
* it has subscribed to

typedef struct _subscriber_queues_t
{
  _q_data_t *q_data;
  struct _subscriber_queues_t next_queue;
}_subscriber_queues_t;
*/


void init_queues_data(void);
void _create_queue(uint32_t data);
void _delete_queue(q_id_t); /* call disconect for subscribers and remove own entry from subscribers's list */
void _wait_queue(uint32_t data);
void _attach_queue(void *data);  /* argument: q_name to attach, [out] param for q_id */
void _enq_queue(uint32_t data);
void _lookup_queue(uint32_t data); /* argument: queue_lookup_t */

/*
* Used by timer module to validate q_handle
* passed by user for posting timer expiry
* messages
*/
queue_handle_t qid_to_q_handle(q_id_t q_id, uint32_t pid);

#endif
